﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AiFrame.InterfaceLib.MVVM;

namespace AiFrame.InterfaceLib.MVP
{
    /// <summary>
    /// Helper class for binding controls to properties.
    /// </summary>
    public static class BindingHelper
    {
        /// <summary>
        /// Sets a binding.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="dataSource">The data source.</param>
        /// <param name="dataMember">The data member.</param>
        /// <param name="formattingEnabled">If true, formatting is enabled.</param>
        /// <param name="dataSourceUpdateMode">The datasource update mode.</param>
        public static void SetBinding(Control control, string propertyName, object dataSource, string dataMember, bool formattingEnabled,
            DataSourceUpdateMode dataSourceUpdateMode)
        {
            Binding binding = new Binding(propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode);
            control.DataBindings.Add(binding);
        }

        /// <summary>
        /// Sets a binding with an optione to enable nullable datetime binding.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="dataSource">The data source.</param>
        /// <param name="dataMember">The data member.</param>
        /// <param name="formattingEnabled">If true, formatting is enabled.</param>
        /// <param name="dataSourceUpdateMode">The datasource update mode.</param>
        /// <param name="isNullableDateTimeBinding">If true, datetime is nullable.</param>
        public static void SetBinding(Control control, string propertyName, object dataSource, string dataMember, bool formattingEnabled,
            DataSourceUpdateMode dataSourceUpdateMode, bool isNullableDateTimeBinding)
        {
            if (isNullableDateTimeBinding)
            {
                NullableDateTimeBinding binding = new NullableDateTimeBinding(propertyName, dataSource, dataMember, formattingEnabled,
                    dataSourceUpdateMode);
                control.DataBindings.Add(binding);
            }
            else
            {
                SetBinding(control, propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode);
            }
        }

    }
}
